<?php

namespace Admin\Controller;

use Admin\Controller\GlobalController;

class PushController extends GlobalController {
	private $Push;
	
	function _initialize(){
		$this->Push = new \Admin\Util\Push("54f91edafd98c5e8cf0009d7", "u6lv7pwpy45vp1sjkf9bfiuzl0qam8ek");
	}
	
	public function index() {
		if(IS_POST){
			$ticker = trim(I("POST.ticker", ""));
			$title = trim(I("POST.title", ""));
			$text = trim(I("POST.text", ""));
			if(empty($ticker)){
				$this->ajaxReturn("", "ticker为空", 0);
			}
			if(empty($title)){
				$this->ajaxReturn("", "title为空", 0);
			}
			if(empty($text)){
				$this->ajaxReturn("", "text为空", 0);
			}
			$notification = intval(I("POST.notification", 0));
			switch ($notification){
				case 1:
					$this->ajaxReturn("", "发送单播通知", 1);
					break;
				case 2:
					$this->ajaxReturn("", "发送广播通知", 1);
					break;
				case 3:
					$this->ajaxReturn("", "发送组播通知", 1);
					break;
				case 4:
					$this->ajaxReturn("", "发送文件播通知", 1);
					break;
				case 5:
					$this->ajaxReturn("", "发送自定义播通知", 1);
					break;
				default:
					$this->ajaxReturn("", "无效通知类型", 1);
					break;
			}
		}else{
			$this->display();
		}
		
	}
	
	public function broadcast(){
		if(IS_POST){
			$type = intval(I("POST.type", 0));//1为news
			$about_id = intval(I("POST.about_id", 0));
			$editor_name = I("POST.editor_name", "");
			$editor_avator = I("POST.editor_avator", "");
            $auto_publish_time = I('post.auto_publish_time') . ' ' . I('post.auto_publish_hour');
			if(empty($type) || empty($about_id)){
				$this->ajaxReturn("", "参数为空", 'tip_error');
			}

            //加入定时任务
            if (I('post.is_crontab') == '1') {
                $cModel = M('crontab');
                $myData = array(
                    'type' => $type,
                    'about_id' => $about_id,
                    'editor_name' => $editor_name,
                    'editor_avator' => $editor_avator,
                );
                $data = array(
                    'auto_publish_time' => strtotime($auto_publish_time),
                    'data' => json_encode($myData),
                    'status' => 0,
                    'identifier' => 'push_broadcast',
                );
                $cModel->add($data);
                $this->ajaxReturn("", "加入队列成功", 'tip_success');
                exit();
            }

			$where = array();
			$where["type"] = array("eq", $type);
			$where["about_id"] = array("eq", $about_id);
			$exist = M("Push")->field("id")->where($where)->find();
			if(empty($exist)){
				$result = array();
				switch ($type){
					case 1:
						$where = array();
						$where["news_id"] = array("eq", $about_id);
						$result = M("News")->where($where)->find();
						$result["editor_name"] = $editor_name;
						$result["editor_avator"] = $editor_avator;
						break;
				}
				
				//发送广播
				$ret = $this->Push->sendAndroidBroadcast($result);
				//{"ret":"SUCCESS","data":{"task_id":"us41283143614810497401"}}
				if(empty($ret)){
					$this->ajaxReturn("", "发送异常", 'tip_error');
				}else{
					try {
						$jsonData = json_decode(trim($ret), true);
						if($jsonData["ret"] == "SUCCESS"){
							$data = array();
							$data["task_id"] = $jsonData["data"]["task_id"];
							$data["type"] = $type;
							$data["about_id"] = $about_id;
							$data["status"] = 1;
							$data["add_time"] = date("Y-m-d H:i:s");
							M("Push")->add($data);
							$this->ajaxReturn("", "发送成功", 'tip_success');
						}else{
							$this->ajaxReturn("", "发送失败", 'tip_error');
						}
					} catch (Exception $e) {
						$this->ajaxReturn("", "json解析异常；ret = " . $ret, 'tip_error');
					}
					
				}
			}else{
				$this->ajaxReturn("", "已发送过", 'tip_error');
			}
		}
	}
	
	private function sendAndroidUnicast($device_tokens){
		$this->Push->sendAndroidUnicast($device_tokens);
	}
	
	// Upload your device tokens, and use '\n' to split them if there are multiple tokens,eg:"aa"."\n"."bb"
	private function sendAndroidFilecast($device_token_list){
		$this->Push->sendAndroidFilecast($device_token_list);
	}
	
	private function sendAndroidGroupcast($filter){
		/*
		 *  Construct the filter condition:
		*  "where":
		*	{
		*		"and":
		*		[
		*			{"tag":"test"},
		*			{"tag":"Test"}
		*		]
		*	}
		*/
		$filter = 	array(
				"where" => 	array(
						"and" 	=>  array(
								array(
										"tag" => "test"
								),
								array(
										"tag" => "Test"
								)
						)
				)
		);
		$this->Push->sendAndroidGroupcast($filter);
	}
	
	// Set your alias here, and use comma to split them if there are multiple alias.
	// And if you have many alias, you can also upload a file containing these alias, then
	// use file_id to send customized notification.
	private function sendAndroidCustomizedcast($alias, $alias_type){
		$this->Push->sendAndroidCustomizedcast($alias, $alias_type);
	}
}